﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MAT.Model;
using System.Windows.Forms;
using Microsoft.Win32;
using System.IO;
using System.Data.SqlClient;

namespace MAT.Controllers
{
    class ctrLoad
    {
        private frmLoad _frmLoad = null;
        Controllers.ctrNotice _ctrNotice = new Controllers.ctrNotice();

        public frmLoad FrmLoad
        {
            get { return _frmLoad; }
            set { _frmLoad = value; }
        }

        //Implement one object to set statup application
        RegistryKey rgKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
        MAT_Context context = MATContext.getContext();

        public void frmLoad_Load()
        {
            rgKey.SetValue("MAT", Application.ExecutablePath.ToString());
            Timer tm = new Timer();
            tm.Interval = 3000;
            tm.Start();
            tm.Enabled = true;
            tm.Tick += timer_tick;
        }

        private void timer_tick(object sender, EventArgs e)
        {
            _frmLoad.Hide();
            Timer t = (Timer)sender;
            t.Stop();

            Timer timerWait = new Timer();
            timerWait.Interval = 100;
            timerWait.Start();
            timerWait.Enabled = true;
            timerWait.Tick += timerWait_tick;
        }

        private void timerWait_tick(object sender, EventArgs e)
        {
            Timer t = (Timer)sender;
            t.Stop();
            if (loginUser() != null)
            {
                splitLogin(loginUser());
                LOG_IN login = context.LOG_IN.FirstOrDefault(log => log.USERNAME == MAT.Controllers.ctrLogin.Username);

                if (login.LOGGED == 0)
                {
                    //Set flag for user (logged in)
                    login.LOGGED = 1;
                    context.SaveChanges();

                    //MAT.Controllers.ctrLogin._frmLogin.txtUsername.Text = 
                    MAT.Controllers.ctrMain._frmMain = new frmMain();
                    MAT.Controllers.ctrMain._frmMain.Hide();
                    
                    MAT.Controllers.ctrNotice._frmNotice = new frmNotice();
                    MAT.Controllers.ctrNotice._frmNotice.timerLoad.Start();
                    MAT.Controllers.ctrNotice._frmNotice.MAT_NotifyIcon.ShowBalloonTip(3000, "Information", "Notice form is actived!", ToolTipIcon.Info);
                    MAT.Controllers.ctrNotice._frmNotice.Hide();
                    //frmNotice fNotice = new frmNotice();
                    //fNotice.timerLoad.Start();
                    //fNotice.MAT_NotifyIcon.ShowBalloonTip(3000, "Information", "Notice form is actived!", ToolTipIcon.Info);
                    //fNotice.Hide();
                    _ctrNotice.load_check_data();
                }
                else
                {
                    MessageBox.Show("Duplicate login information");
                    frmLogin f = new frmLogin();
                    f.ShowDialog();
                }
            }
            if (loginUser() == null)
            {
                //Clear the number of request is saved in system
                //StreamWriter ghi = new StreamWriter(Application.StartupPath + @"\Log.txt");
                //ghi.Flush();
                //ghi.Write("");
                //ghi.Close();

                frmLogin f = new frmLogin();
                f.ShowDialog();
            }
        }

        private string loginUser()
        {
            if (!File.Exists(Application.StartupPath + @"\Log.txt"))
            {
                File.Create(Application.StartupPath + @"\Log.txt");
                return null;
            }
            else
            {
                StreamReader readFile = new StreamReader(Application.StartupPath + @"\Log.txt");
                string output = readFile.ReadLine();
                readFile.Close();
                return output;
            }
        }

        private void splitLogin(string input)
        {
            int first = input.IndexOf("|");
            int last = input.LastIndexOf("|");
            MAT.Controllers.ctrLogin.Username = input.Substring(0, first).ToUpper();
            MAT.Controllers.ctrLogin.priority = Convert.ToInt16(input.Substring(first + 1, 1));
            MAT.Controllers.ctrLogin.employee_ID = input.Substring(last + 1);
        }

    }
}
